<?php
require_once("constants.php");

class PVC_Controller_Generator{

	private $project_name;
	private $pvc_home;
	private $project_home;
	private $controller_name;
	private $verbose;
	private $from_prj_gen;

	/**
	 * Constructor
	 * @param the project name
	 * @param the controller name
	 */
	public function __construct($cname, $project=null, $verbose=false, $from_prj_gen=false){
		if($project == null){ 
			$this->project = dirname(".");
		} else {
			$this->project_name = $project;
		}
		$this->controller_name = $cname;
		$this->verbose = $verbose;
		$this->pvc_home = __FILE__  . "..". DIRECTORY_SEPARATOR;
		$this->project_home = $project . DIRECTORY_SEPARATOR;
		$this->from_prj_gen = $from_prj_gen;
	}

	/**
	 * Generate the controller
	 */
	public function generate(){
		
		echo(" => generating the {$this->controller_name} controller\n");
		
		$controller_file_name = strtolower($this->controller_name)."_controller.class.php";
		$controller_src = $this->pvc_home.TEMPLATES.DIRECTORY_SEPARATOR.T_CONTROLLERS.DIRECTORY_SEPARATOR.CONTROLLER_TEMPLATE;
		if($this->from_prj_gen){
			$controller_dest = $this->project_home.APP.DIRECTORY_SEPARATOR.$controller_file_name;
		} else {
			$controller_dest = APP.DIRECTORY_SEPARATOR.$controller_file_name;
		}
		
		$fw = fopen($controller_dest, "w");
		if($this->verbose){ echo("    : file - $controller_file_name\n");}
		fwrite($fw, $this->get_controller($this->controller_name, $this->project_name));
		fclose($fw);
		

	}

	private function get_controller($cname, $appname, $actions=array()){
		
		$app_name = ucfirst(strtolower($appname));
		$controller_name_lc = strtolower($cname);
		$controller_name = ucfirst($controller_name_lc)."_Controller";		
		$str = <<<CONTROL
<?php
		
class $controller_name extends $app_name {
			
	private \$view_vars;
	private \$controller;

	public function index(){

		/*
		 * put all application code for the index action here...
		 */
		\$this->display(\$this->controller, __FUNCTION__, \$this->view_vars);
	}\n\n
CONTROL;

		if(isset($actions)) {
			// add all actions that the user requested
		}

		$str .= <<<CONTROL
	public function __construct(\$get_vars=array()){
		parent::construct(\$get_vars);
		\$this->controller = "$controller_name_lc";
	}

}

?>
CONTROL;
	
	return $str;
	
	}



	/**
	 * It is imperative the user runs this script from 
	 * the root of the project directory
	 */
	public function is_correct_directory(){

					$current = opendir(".");
					while($entry = readdir($current)){
									$dir_array[] = $entry;
					}

					if(in_array( APP, $dir_array) && in_array( VIEWS, $dir_array)){
									echo(" => you appear to be in the correct directory\n");
									return true;

					} else {
									echo(" => please run this script from the root of the project directory\n");
									return false;
									}
	}
}

		//	$regex = "/^!!!require!!!/";
		//	$regex2 = "/^!!!classname!!!/";
		//	while(!feof($fr)) {
		//		$buffer = fgets($fp, 4096);
		//		if(preg_match($regex, $buffer)){
		//			$require = "require_once(..".DIRECTORY_SEPARATOR.LIB.DIRECTORY_SEPARATOR.strtolower($this->project_name).".class.php);\n\n";
		//			fwrite($fw, $require);
		//		}else if(preg_match($regex2, $buffer)){
		//			$class_string = "class ".ucfirst($this->controller_name)."_Controller extends " . ucfirst($this->project_name)."\n" ;
		//		} else {
		//						fwrite($fw, $buffer);
		//		}
		//	}
		//}
		//fclose($fp);

?>
